package com.binance.notification.web.service.abnormaltrade.job;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.binance.autojob.annotation.MyScheduledJob;
import com.binance.notification.web.service.abnormaltrade.IAbnormalPriceService;
import com.binance.notification.web.service.abnormaltrade.log.LoggerUtils;
import com.binance.notification.web.service.abnormaltrade.sharding.JobShardingService;
import com.xxl.job.core.util.ShardingUtil;

/**
 *
 * 异动提示窗,需求参照 https://confluence.fdgahl.cn/pages/viewpage.action?pageId=5178283
 */
@Component
public class AbnormalPriceNoticeJob {

    private static final org.slf4j.Logger log = LoggerUtils.getLogger(AbnormalPriceNoticeJob.class);

    @Autowired
    private IAbnormalPriceService abnormalPriceService;

    @Autowired
    private JobShardingService jobShardingService;

    @Value("${price.abnormal.alert.enabled:false}")
    private boolean abnormalPriceAlertEnabled;

    /**
     * 30s轮询一次Btc异常交易
     */
    @MyScheduledJob(cron = "0/5 * * * * ?", author = "wangxiao", alarmEmail = "louis.wang@binance.com",
            jobDesc = "5s轮询一次Btc异常交易")
    public void btc() {
        if (!abnormalPriceAlertEnabled) {
            log.info("'abnormalPriceAlertEnabled' not enabled");
            return;
        }
        try {
            log.info("AbnormalPriceNoticeJob.generateAbnormalBtcTradeNotice start");
            // 分片逻辑
            ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
            Set<String> symbolSet =
                    jobShardingService.getShardingSymbolSet(shardingVO.getIndex(), shardingVO.getTotal(), "BTC");
            log.info(
                    "AbnormalPriceNoticeJob.generateAbnormalUsdtTradeNotice getShardingSymbolSet 当前分片序号 = {}, 总分片数 = {}, size={},symbolSet={}",
                    shardingVO.getIndex(), shardingVO.getTotal(), symbolSet.size(), symbolSet);
            abnormalPriceService.generateBtcAbnormalTradeNotice(symbolSet);
            log.info("AbnormalPriceNoticeJob.generateAbnormalBtcTradeNotice end");
        } catch (Exception e) {
            log.error("AbnormalPriceNoticeJob.generateAbnormalBtcTradeNotice Exception", e);
        }
    }


    /**
     * 30s轮询一次Usdt异常交易
     */
    @MyScheduledJob(cron = "0/5 * * * * ?", author = "wangxiao", alarmEmail = "louis.wang@binance.com",
            jobDesc = "5s轮询一次Usdt异常交易")
    public void usdt() {
        if (!abnormalPriceAlertEnabled) {
            log.info("'abnormalPriceAlertEnabled' not enabled");
            return;
        }
        try {
            log.info("AbnormalPriceNoticeJob.generateAbnormalUsdtTradeNotice start");
            // 分片逻辑
            ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
            Set<String> symbolSet =
                    jobShardingService.getShardingSymbolSet(shardingVO.getIndex(), shardingVO.getTotal(), "USDT");
            log.info(
                    "AbnormalPriceNoticeJob.generateAbnormalUsdtTradeNotice getShardingSymbolSet 当前分片序号 = {}, 总分片数 = {}, size={},symbolSet={}",
                    shardingVO.getIndex(), shardingVO.getTotal(), symbolSet.size(), symbolSet);
            abnormalPriceService.generateUsdtAbnormalTradeNotice(symbolSet);
            log.info("AbnormalPriceNoticeJob.generateAbnormalUsdtTradeNotice end");
        } catch (Exception e) {
            log.error("AbnormalPriceNoticeJob.generateAbnormalUsdtTradeNotice Exception", e);
        }
    }

}
